/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logic;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Freshman
 */
public class SaveProp {

    private Varpool vap;
    private BufferedOutputStream stream = null;
    private File config = new File("hcc.properties");

    ;
    
    public SaveProp(Varpool variables) {
        this.vap = variables;

        if (config.exists()) {
            updateCfg(config);
        } else {
            createCfg();
        }
    }

    private void updateCfg(File config) {

        try {
            stream = new BufferedOutputStream(new FileOutputStream(config));
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
        try {
            vap.getProperties().store(stream, null);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        closeStream();
    }

    private void createCfg() {
        vap.getProperties().setProperty("Datenbank", vap.getDbname());
        vap.getProperties().setProperty("Loginname", vap.getLoginname());
        vap.getProperties().setProperty("Passwort", vap.getPassword());
        try {
            stream = new BufferedOutputStream(new FileOutputStream(config));
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        }
        try {
            vap.getProperties().store(stream, null);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        closeStream();
    }

    private void closeStream() {
        try {
            stream.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}
